﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Flare.Pages.Hards
{
    public partial class HardType : UserControl, ITypicalPage
    {
        public string name
        {
            get { return "Типы оборудования"; }
        }

        public HardType()
        {
            InitializeComponent();
        }

        private void TypeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (TypeList.SelectedIndex == -1)
            {
                ButtonSave.Visibility = System.Windows.Visibility.Collapsed;
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ButtonClose.Visibility = System.Windows.Visibility.Collapsed;
                ButtonAdd.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                ButtonSave.Visibility = System.Windows.Visibility.Visible;
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                ButtonClose.Visibility = System.Windows.Visibility.Visible;
                ButtonAdd.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var data = new Flare.DB.Entity.EquipmentType()
            {
                type = hardtype.Text
            };

            try
            {
                new DB.equipment_types().Create(data);
                hardtype.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_typesDataSet");
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (TypeList.SelectedIndex == -1) return;
            var data = new Flare.DB.Entity.EquipmentType()
            {
                id_type = (int)TypeList.SelectedValue,
                type = hardtype.Text
            };

            try
            {
                new DB.equipment_types().Edit(data);
                TypeList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_typesDataSet");
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (TypeList.SelectedIndex == -1) return;
            try
            {
                new DB.equipment_types().Delete((int)TypeList.SelectedValue);
                TypeList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_typesDataSet");
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            TypeList.SelectedItem = null;
        }
    }
}
